package com.moyu.oasys.chat.listner;

import com.moyu.common.core.constants.RabbitMQConstants;
import com.moyu.oasys.chat.container.SessionContainer;
import com.moyu.oasys.chat.entity.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;

import java.io.IOException;

/**
 * @Author 赵彦博
 * @Date 2024/12/3 20:34
 */
@Component
public class WebSocketEventListener {

    @RabbitListener(queues = RabbitMQConstants.OASYS_WORK_QUEUE) // 假设队列名为chat-queue，根据实际情况调整
    public void handleMessage(String message) {
        TextMessage textMessage = new TextMessage(message);
        SessionContainer.values().forEach(session -> {
            try {
                session.sendMessage(textMessage);
            } catch (IOException e) {
                // 处理发送消息失败的情况
            }
        });
    }

}
